import java.util.HashMap;
import java.util.Stack;

public class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Stack<Integer> stack = new Stack<>();
        HashMap<Integer, Integer> hashmap = new HashMap<>();
        for (int i = 0; i < nums2.length; i++) {
            while (!stack.empty() && stack.peek() < nums2[i]) {
                int temp = stack.pop();
                hashmap.put(temp, nums2[i]);
            }

            stack.push(nums2[i]);

        }
        int[] res = new int[nums1.length];
        for (int i = 0; i < nums1.length; i++) {
            res[i] = hashmap.getOrDefault(nums1[i], -1);
        }
        return res;
    }

}
